﻿using MCPlatform.UI.Components;
using MCPlatform.UI.Controls.Converts;
using MCPlatform.UI.Controls.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Caliburn.Micro;
using System.Data;
using Panuon.WPF.UI;
using System.Windows.Controls;
using MCPlatform.Shared.DAL;

namespace MCPlatform.ViewModels.Shell
{
    public class AlarmViewModel : ViewModelBase
    {

        private DataTable _table = new DataTable();

        public DataTable Table
        {
            get => _table; set => Set(ref _table, value);
        }

        private bool isChecking = false;
        public CommandBase CommandCheck { get; set; }

        public AlarmViewModel()
        {

            this.CommandCheck = new CommandBase(GetDataAcess2Table);

        }

        void GetDataAcess2Table(object o)
        {
            try
            {
                if (isChecking)
                {
                    return;
                }
                isChecking = true;
                //传递参数数组过来，分别为设备名，查询开始时间，查询结束时间，是否快捷查询
                var values = (Object[])o;

                DataAccess dataAccess = new DataAccess();

                string startDate = DateTime.Now.ToString("yyyy-MM-dd");
                string endDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");


                if (values[0].ToString() == "自定义时间")
                {
                    var startD = GetDateTime(values[2]);
                    var endD = GetDateTime(values[3]);

                    if (endD - startD > TimeSpan.FromDays(7))
                    {
                        Toast.Show("结束时间和开始时间跨度过长，请重新选择！", targetWindow: ToastWindow.MainWindow);
                        return;
                    }

                    startDate = GetDateTimeToString(values[2]);
                    endDate = GetDateTimeToString(values[3]);

                    if (startDate == string.Empty || endDate == string.Empty)
                    {
                        return;
                    }

                }



                string strSql = $"select * from [NLog].[dbo].[Log] where Level = '{values[1].ToString()}' and Logged > '{startDate}' and Logged < '{endDate}' order by Logged desc";
                if (Table != null)
                {
                    Table.Clear();
                    Table = dataAccess.GetDatas(strSql);
                }

               
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                isChecking = false;

            }
         


        }

        private string GetDateTimeToString(object o)
        {
            var startPanel = o as StackPanel;
            if (startPanel is null)
            {
                return string.Empty;
            }
            foreach (var item in startPanel.Children)
            {
                if (item is DateTimePicker)
                {
                    DateTimePicker timePicker = (DateTimePicker)item;
                    DateTime date = (DateTime)timePicker.SelectedDateTime!;
                    return date.ToString("yyyy-MM-dd HH:mm:ss");
                }
            }

            return string.Empty;
        }

        private DateTime GetDateTime(object o)
        {
            var startPanel = o as StackPanel;
            if (startPanel is null)
            {
                return DateTime.Now;
            }
            foreach (var item in startPanel.Children)
            {
                if (item is DateTimePicker)
                {
                    DateTimePicker timePicker = (DateTimePicker)item;
                    return (DateTime)timePicker.SelectedDateTime!;                   
                }
            }

            return DateTime.Now;
        }

    }
}